//
//  NSString+ChatRoom.m
//  LeTVMobileDataModel
//
//  Created by wangduan on 15/3/3.
//  Copyright (c) 2015年 Kerberos Zhang. All rights reserved.
//

#import "NSString+ChatRoom.h"
#import <CommonCrypto/CommonDigest.h>
#import "NSString+HTTPExtensions.h"
#import "NSObject+ObjectEmpty.h"

@implementation NSString (ChatRoom)

// 时间戳 《－ 1990-02-11
+ (NSString *)getIntervalFromDateStr:(NSString *)dateStr{

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *date = [dateFormatter dateFromString:dateStr];
    
    NSTimeInterval timeInterval = [date timeIntervalSince1970];
    return [NSString stringWithFormat:@"%ld",(long)timeInterval];
}

+ (NSString *)getYearMonthDay:(NSDate *)date{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
    //zzz表示时区，zzz可以删除，这样返回的日期字符将不包含时区信息。
    
    //    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss zzz"];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    
    NSString *destDateString = [dateFormatter stringFromDate:date];
    
    return destDateString;
}
/*
 * 获得一个HH:mm(20:24)格式的字符串，需要传一个时间戳的timeString字符串。
 */
+ (NSString *)getTimeStringFromAnotherTimeString:(NSString *)timeString
{
    if ([NSString isBlankString:timeString]) {
        return @"";
    }else{
        NSTimeInterval timeInterval = timeString.doubleValue;
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"HH:mm"];
        NSString * newTimeString = [formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:timeInterval]];
        return newTimeString;
    }
}
/*
 * 获得当前设备日期时间戳（121231231）的字符串。
 */
+ (NSString *)getTimeIntervalFromSystemTime
{
    NSTimeInterval timeInterval = [[NSDate date] timeIntervalSince1970];
    return [NSString stringWithFormat:@"%.0f",timeInterval];
}

#pragma mark --时移相关处理，timeShift
/*
 * 获得时间 hh:mm:ss（20:20:20）格式的字符串，需要传入一个double型的时间戳
 */
+ (NSString *)getTimeStringFromTimeInterval:(double)timeInterval
{
    NSDate *playbackDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSEraCalendarUnit |NSYearCalendarUnit | NSMonthCalendarUnit| NSDayCalendarUnit| NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:playbackDate];
    return [NSString stringWithFormat:@"%02ld:%02ld:%02ld",(long)components.hour,(long)components.minute,(long)components.second];
}

+(NSString *)md5:(NSString *)str{
    const char *cStr =[str UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(cStr, (CC_LONG)strlen(cStr), result);
    return [[NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
             result[0], result[1], result[2], result[3],
             result[4], result[5], result[6], result[7],
             result[8], result[9], result[10], result[11],
             result[12], result[13], result[14], result[15]
             ] lowercaseString];
}

+ (NSString*)safeString:(NSString*)srcStr{
    if ([NSObject empty:srcStr]) {
        return @"";
    }
    
    return [NSString stringWithFormat:@"%@", srcStr];
}

+ (NSString *)convertDateyHd:(NSString *)aInterVal {
    long long time  = [aInterVal longLongValue];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *theday = [NSDate dateWithTimeIntervalSince1970:time];
    NSString *day = [dateFormatter stringFromDate:theday];
    return day;
}

@end
